import data

library(tidyverse)
library(lubridate)
library(sf)
library(tmap)

run_day_12038_liz22_del_14_33 <- st_read("/Users/rof011/coralseed/code/2021_2022_HighPRT_sim_examples/run_day_12038_liz22_del_14_33/run_day_12038_liz22_del_14_33.json") |> 
  st_transform(20353)
## Reading layer `run_day_12038_liz22_del_14_33' from data source 
##   `/Users/rof011/coralseed/code/2021_2022_HighPRT_sim_examples/run_day_12038_liz22_del_14_33/run_day_12038_liz22_del_14_33.json' 
##   using driver `GeoJSON'
## Simple feature collection with 605000 features and 3 fields
## Geometry type: POINT
## Dimension:     XYZ
## Bounding box:  xmin: 145.4446 ymin: -14.71132 xmax: 145.4806 ymax: -14.69039
## z_range:       zmin: 3.55 zmax: 3.55
## Geodetic CRS:  WGS 84
run_day_12038_liz22_del_14_33_subset <- run_day_12038_liz22_del_14_33 |> 
  dplyr::filter(id %in% seq(0:500)) |> 
  mutate(daytime=ymd_hms(time)) |> 
  mutate(hour=hour(daytime)) |> 
  na.omit()

### several points aren't parsing as they are ymd not ymd_hms
### run na.omit to remove (always at the end of the timeseries)
#which(is.na(run_day_12038_liz22_del_14_33_subset$daytime))
#run_day_12038_liz22_del_14_33_subset[7018,3]
#run_day_12038_liz22_del_14_33_subset[12221,3]
library(tmap)
library(tmaptools)
library(coralseed)

seascape <- seascape_probability(reefoutline=reef_map, habitat=benthic_map)

tmap_mode("plot")

anim <- tm_shape(seascape) +
  tm_polygons("class", alpha=0.2, legend.show=FALSE) +
tm_shape(run_day_12038_liz22_del_14_33_subset, is.master=TRUE) +
  tm_dots("hour", pal="-Spectral", n=24, shape=21, border.lwd=0.1, 
          border.col="black", legend.show=TRUE) +
tm_facets(along="hour", free.coords=FALSE, free.scales=FALSE) +
  tmap_options(max.categories=1000) 

tmap_animation(
  anim,
  filename = "test.gif",
  width = 1200,
  height = 800,
  delay = 20,
   fps =  2,
   loop = TRUE
)
## Creating frames
## =======
## ===
## ===
## ====
## ===
## ===
## ====
## ===
## ===
## ====
## ===
## ===
## ====
## ===
## ===
## ====
## ===
## ===
## ====
## ===
## ===
## ====
## ===
## 
## Creating animation
## Animation saved to /Users/rof011/coralseed/code/test.gif

map particle tracks

tmap_mode("view")
## tmap mode set to interactive viewing
tm_basemap("Esri.WorldTopoMap") +
tm_shape(run_day_12038_liz22_del_14_33_subset, is.master=TRUE) +
  tm_dots("hour", pal="-Spectral", n=24,  legend.show=TRUE) +
tm_view(view.legend.position = c("right", "bottom"))